package com.qf.hmadmin.sys.user.controller;

import com.qf.entity.sys.SysUser;
import com.qf.hmadmin.sys.user.param.LoginParam;
import com.qf.hmadmin.sys.user.service.impl.SysUserServiceImpl;
import com.qf.utils.R;


import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import javax.servlet.http.HttpSession;

import java.util.HashMap;
import java.util.UUID;

@RestController
@RequestMapping("/user")
@Slf4j
public class SysUserController {

    @Resource
    private SysUserServiceImpl sysUserServiceImpl;





    @PostMapping("/login")
    public R login(@RequestBody LoginParam loginParam, HttpSession session){

        SysUser login = sysUserServiceImpl.login(loginParam);

        session.setAttribute("userInfo",login);
        HashMap<String, String> map = new HashMap<>();
map.put("token", UUID.randomUUID().toString());

        return R.ok(map);
    }

    @GetMapping("/info")
public R info(HttpSession session){
    SysUser userInfo =(SysUser) session.getAttribute("userInfo");
if (userInfo == null){
return  R.fail(501,"session失效，请重新登录");
}
return  R.ok(userInfo);
    }


    @PostMapping("/logout")
    public R logout(HttpSession session){
        session.invalidate();
        return  R.ok();
    }


}
